package 每日一题;

import java.util.Iterator;
import java.util.LinkedList;

public class PeekingIterator implements Iterator<Integer> {

    LinkedList<Integer> list;
    public PeekingIterator(Iterator<Integer> iterator) {
        // initialize any member here.
        list = new LinkedList<>();
        while (iterator.hasNext()){
            list.add(iterator.next());
        }
    }

    // Returns the next element in the iteration without advancing the iterator.
    public Integer peek() {
         return list.peek();
    }

    // hasNext() and next() should behave the same as in the Iterator interface.
    // Override them if needed.
    @Override
    public Integer next() {
         return list.removeFirst();
    }

    @Override
    public boolean hasNext() {
        return list.size()!=0;
    }
}
